﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;
using PPAP.Model;

namespace PPAP.Web.Part
{
    public partial class SampleCard : PageBase
    {
        SamplecardBO sbo = new SamplecardBO();
        PPARBO pbo = new PPARBO();
        SampleCardTaskBO stbo = new SampleCardTaskBO();
        public string RID { set; get; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["RID"] != null)
                {
                    RID = Request.QueryString["RID"].ToString();
                    DataSet ds = sbo.GetList(" pparid=" + RID);
                    string ru = "";
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        DataRow dr = ds.Tables[0].NewRow();
                        dr[2] = "No data";
                        ds.Tables[0].Rows.Add(dr);
                        GridView1.Columns[4].Visible = false;
                        GridView1.Columns[9].Visible = false;
                        //GridView1.Columns[10].Visible = false;
                        //GridView1.Columns[11].Visible = false;
                        tr.Visible = false;
                    }
                    else
                    {
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            ru += dr["RESULT"].ToString();
                        }
                        if (ru == "")
                            tr.Visible = false;
                    }
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                    PPAR pp = pbo.GetModel(int.Parse(Request.QueryString["RID"].ToString()));
                    if (pp != null)
                    {
                        if (pp.STATUS >= 3&&pp.STATUS!=4&&pp.STATUS<6)
                            btnAdd.Visible = true;
                        else
                            btnAdd.Visible = false;
                    }
                }
                if (Request.QueryString["his"] != null && Request.QueryString["his"].ToString() == "1") //从histroy页面跳转过来
                {
                    GridView1.Columns[11].Visible = false;
                    btnAdd.Visible = false;
                }
                
            }
            if (Request.QueryString["sid"] != null) //终止
            {
                Delete(Request.QueryString["sid"].ToString());
            }
        }

        private void Delete(string id)
        {
            Model.Samplecard sa = sbo.GetModel(int.Parse(id));
            if (sa != null)
            {
                sa.STATUS = 9;
               int num= sbo.Update(sa);
               if (num > 0)
               {
                   AddSampleCardTask("Termination sample card", int.Parse(id));
                   AlertRedirect("Termination succeed", "SampleCard.aspx?his=0&rid="+Request.QueryString["rid"].ToString());
               }
            }
        }

        public string Status(string status)
        {
            string font = "";
            switch (status)
            {
                case "0":
                    font = "Created";
                    break;
                case "1":
                    font = "Circulating";
                    break;
                case "2":
                    font = "Circulating(<font color='red'>Reject</font>)";
                    break;
                case "9":  //终止
                    font = "<font color='red'>termination</font>";
                    break;
                default:
                    font = "Decided";
                    break;
            }
            return font;
            
        }

        /// <summary>
        /// 保存流程记录
        /// </summary>
        /// <param name="Reamrk"></param>
        private void AddSampleCardTask(string Reamrk, int id)
        {
            SampleCardTask task = new SampleCardTask();
            task.CREATETIME = DateTime.Now;
            task.CREATEUSER = CurrentUser.Id;
            task.REAMRK = Reamrk;
            task.SID = id;
            stbo.Add(task);
        }
    }
}